package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.Result;
import com.example.springboot.entity.TRole;
import com.example.springboot.entity.TUser;
import com.example.springboot.service.ITRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lml
 * @since 2023-01-10
 */
@RestController
@RequestMapping("/t-role")
public class TRoleController {

    @Resource
    private ITRoleService service;

    @GetMapping("farmatRoleName")
    public Result ById(Integer id){
        return new Result(service.getById(id));
    }

    @GetMapping
    public Result queryall(){
        return new Result<>(service.list());
    }

    @GetMapping("query")
    public Result query(TRole role, Page page){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("rolename",role.getRolename());
        return new Result(service.page(page,queryWrapper));
    }
    @DeleteMapping("{id}")
    public Result delbyId(@PathVariable Integer id){
        System.out.println("要删除的id"+id);
        return new Result(service.removeById(id));
    }
    @PostMapping
    public Result SaveAndUpdata(TRole role){
        boolean b = service.saveOrUpdate(role);
        return new Result(b);
    }
    @GetMapping("chackname")
    public Result Chackname(String rolename){
        Map<String,Object> map=new HashMap<>();
        map.put("rolename",rolename);
        List<TRole> tRoles = service.listByMap(map);
        if (tRoles.size()>0){
            return new Result(true);
        }
        return new Result(false);
    }
}

